{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banner_Top_06.06.18.jpg?raw=true)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Import Python tools\n",
    "from IPython.display import HTML\n",
    "from IPython import display \n",
    "from ipywidgets import widgets\n",
    "from ipywidgets import interactive\n",
    "\n",
    "import calculate\n",
    "import numpy as np\n",
    "import pandas as pd\n",
    "%matplotlib inline\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "from plotly.offline import init_notebook_mode, iplot\n",
    "import plotly.plotly as py\n",
    "import plotly.graph_objs as go\n",
    "\n",
    "\n",
    "init_notebook_mode(connected=True)         \n",
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")\n",
    "hide_me =''\n",
    "HTML('''<script>\n",
    "code_show=true; \n",
    "function code_toggle() {\n",
    "  if (code_show) {\n",
    "    $('div.input').each(function(id) {\n",
    "      el = $(this).find('.cm-variable:first');\n",
    "      if (id == 0 || el.text() == 'hide_me') {\n",
    "        $(this).hide();\n",
    "      }\n",
    "    });\n",
    "    $('div.output_prompt').css('opacity', 0);\n",
    "  } else {\n",
    "    $('div.input').each(function(id) {\n",
    "      $(this).show();\n",
    "    });\n",
    "    $('div.output_prompt').css('opacity', 1);\n",
    "  }\n",
    "  code_show = !code_show\n",
    "} \n",
    "$( document ).ready(code_toggle);\n",
    "</script>''')\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction\n",
    "This notebook is an extension of, and in some ways, a more transparent (if only by putting the inflation adjustments front and center) version of the LA Times article available from the March 17, 2017 story [\"Wages rise on California farms. Americans still don’t want the job\"](http://www.latimes.com/projects/la-fi-farms-immigration/). In this notebook, we've changed it so you may apply the inflation adjusments yourself, as well as compare multiple state wages together. Additionally, we've added the \"Data processing, hosting and related services\" industries to the data set for a comparison of wage growth between the two."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Adjusts dollar figures for inflation using the Consumer Price Index.\n",
    "\n",
    "Note: This function and the cpi values are taken directly from the LA times article. The\n",
    "one change we have made is that our inflation adjustmetns are not just to 2015 (although, \n",
    "that might be the only year you're interested in for inflation adjustments) \n",
    "\"\"\"\n",
    "cpi = {\n",
    "  \"1990\": 130.658,\n",
    "  \"1991\": 136.167,\n",
    "  \"1992\": 140.308,\n",
    "  \"1993\": 144.475,\n",
    "  \"1994\": 148.225,\n",
    "  \"1995\": 152.383,\n",
    "  \"1996\": 156.858,\n",
    "  \"1997\": 160.525,\n",
    "  \"1998\": 163.008,\n",
    "  \"1999\": 166.583,\n",
    "  \"2000\": 172.192,\n",
    "  \"2001\": 177.042,\n",
    "  \"2002\": 179.867,\n",
    "  \"2003\": 184.0,\n",
    "  \"2004\": 188.908,\n",
    "  \"2005\": 195.267,\n",
    "  \"2006\": 201.558,\n",
    "  \"2007\": 207.344,\n",
    "  \"2008\": 215.254,\n",
    "  \"2009\": 214.565,\n",
    "  \"2010\": 218.076,\n",
    "  \"2011\": 224.93,\n",
    "  \"2012\": 229.6,\n",
    "  \"2013\": 232.962,\n",
    "  \"2014\": 236.712,\n",
    "  \"2015\": 237.0\n",
    "}\n",
    "\n",
    "def convert_to_year(value, year, to_year):\n",
    "    return (value * cpi[str(to_year)]) / cpi[str(year)] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read in state-level data\n",
    "# Because the data set takes a while to create, this is a version that we're hosting on our cloud. \n",
    "# This is slightly modified to remove the inflation correction in pre processing, as we're going to \n",
    "# show it directly here. \n",
    "state_df = pd.read_csv(\"https://swift-yeg.cloud.cybera.ca:8080/v1/AUTH_233e84cd313945c992b4b585f7b9125d/callysto-open-data/transformed_state.csv\", dtype={\"area_fips\": \"str\"})\n",
    "state_df.head()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The above table displays the first few entries of the aggregated and filtered data table from the LA times analysis. We've also added a \"data\" industry group for future analysis, but the focus of this notebook is just on crops. What's most important to see here is that this data table contains data from more states than California. This means we can now compare multiple states against each other."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reproducing LA Times Results\n",
    "\n",
    "Our first task is to reproduce the analysis of the LA Times here. Before we start comparing different states/applying different calculations. First, we shamelessly copy and paste the LA Times analysis from the notebook available on their GitHub repo.  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Filter that down to just California crop workers\n",
    "# Here 06 is the beginning code of Califnornia. \n",
    "ca_state_df = state_df[state_df.area_fips.str.startswith(\"06\")]\n",
    "# Now we're just filtering down to only crops, and we're indexing it by year.\n",
    "# an index can be thought of as the \"row number\" of the data table \n",
    "ca_state_crops = ca_state_df[ca_state_df.industry_group == 'crops'].set_index(\"year\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "First, see the average anual pay of California crop workers in the cell below"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ca_state_crops.at[2015, \"avg_annual_pay_2015\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The next step is to compare this value to the average wage crop workers were paid in 1990"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ca_state_crops.at[1990, \"avg_annual_pay_2015\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "While this looks like a significant improvement, we have to remember that 1990 dollars are worth more than our inflated 2015 dollars. \n",
    "\n",
    "---\n",
    "### Try it Out\n",
    "\n",
    "1. Using the cells above, what pieces would you have to change to see wages in California in a different year? Try it out.\n",
    "\n",
    "---\n",
    "\n",
    "To make a more fair comparison, we have to first adjust the value above for inflation manually below. We note that in the LA Times article, the inflation adjustment was done behind the scenes. Not with any intention to be misleading in their analysis - it was for convenience for their presentation of data. The cells below adjust these values for comparison."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wage_1990 = ca_state_crops.at[1990, \"avg_annual_pay_2015\"]\n",
    "wage_2015 = ca_state_crops.at[2015, \"avg_annual_pay_2015\"]\n",
    "\n",
    "adjusted_wage_1990 = convert_to_year(value=wage_1990, year=1990, to_year=2015)\n",
    "\n",
    "print(\"1990 Wage = $\", adjusted_wage_1990, \"\\n2015 Wage = $\", wage_2015)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where here we now see that even after adjusting for inflation, California farmers have seen a wage increase from 1990. \n",
    "\n",
    "---\n",
    "### Try it out\n",
    " - Use the cells above to compare different years to the 2015 wages. Which values will you have to change to adjust inflation and grab correct wages?\n",
    " \n",
    " ---\n",
    "\n",
    "The next step is to reproduce the percent change in wages (inflation adjusted) from 2010 to 2015, and compare it to the wage increases across the state. We can do this like the LA Times using their calculate module below, which is where the `percent_change` function comes from. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "wage_increase_crop=calculate.percentage_change(\n",
    "    convert_to_year(ca_state_crops.at[2010, \"avg_annual_pay_2015\"], 2010,2015),\n",
    "    ca_state_crops.at[2015, \"avg_annual_pay_2015\"]\n",
    ")\n",
    "print(\"Crop percent change = \", wage_increase_crop, \"%\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ca_state_overall = ca_state_df[ca_state_df.industry_group == 'total'].set_index(\"year\")\n",
    "\n",
    "state_wage_increase =  calculate.percentage_change(\n",
    "    convert_to_year(ca_state_overall.at[2010, \"avg_annual_pay_2015\"], 2010,2015),\n",
    "    ca_state_overall.at[2015, \"avg_annual_pay_2015\"]\n",
    ")\n",
    "print(\"State percent change = \", state_wage_increase, \"%\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where this has reproduced the LA Times analysis on that section\n",
    "\n",
    "---\n",
    "### Try it out\n",
    "- What was the percent change in inflation adjusted wages between 1990 and 2015? What about non-inflation adjusted?\n",
    "\n",
    "---\n",
    "\n",
    "\n",
    "We then see that the crop worker wages have increased at (slightly) more than twice the rate of wages across California in the same time frame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(wage_increase_crop/state_wage_increase)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Reproducing the Graph\n",
    "\n",
    "We are now in a position to reproduce the graph from the LA Times article as seen below. The first line of code below adds another column to our data table of the inflation adjusted wages before we plot it. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reproduce the graph\n",
    "# First we create an inflation adjusted column.\n",
    "# all lambda does for our purposes a way to address a function to pass\n",
    "# row values to our function.  Row.name is the year as that's what we reindexed\n",
    "# our data frame to earlier .\n",
    "ca_state_crops[\"inflation_adj\"] = ca_state_crops.apply(lambda row: \n",
    "                                                       convert_to_year(row.avg_annual_pay_2015, \n",
    "                                                                       row.name, \n",
    "                                                                       2015),\n",
    "                                                       axis=1)\n",
    "\n",
    "ca_state_crops.reset_index().plot(kind='line',\n",
    "                                  x='year', \n",
    "                                  y=['inflation_adj'], \n",
    "                                  figsize=(10, 6),\n",
    "                                  )\n",
    "plt.legend([\"Adjusted to 2015 dollars\"])\n",
    "plt.ylabel(\"US Dollars\")\n",
    "plt.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where this has reproduced the graph of the LA Times article, even though it's perhaps less aesthetically pleasing.\n",
    "\n",
    "---\n",
    "### Try it out\n",
    "\n",
    "- Try adding the non inflation adjusted plot to the graph above. This can be done in two steps\n",
    "    1. Find the following line in the code above \n",
    "    ```python\n",
    "    y = ['inflation_adj']\n",
    "    ```\n",
    "    and change it to\n",
    "    ```python\n",
    "    y = ['inflation_adj', avg_annual_pay_2015]\n",
    "    ```\n",
    "    2. Find the following line in the code above\n",
    "    ```python\n",
    "       plt.legend([\"Adjusted to 2015 dollars\"])\n",
    "    ```\n",
    "    and change it to\n",
    "    ```python\n",
    "       plt.legend([\"Adjusted to 2015 dollars\", \"your label here\"])\n",
    "    ```\n",
    "\n",
    "---\n",
    "\n",
    "But now this might get more interesting if we begin to compare crop wages across different states directly. From this point, we are now diverging from the LA Times analysis. The information of all states can be displayed using the widget below by adjusting the drop down menus.\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hide_me\n",
    "\n",
    "def make_df(state, kind):\n",
    "    out_df = state_df[state_df.area_title.str.match(state)]\n",
    "    out_df = out_df[out_df.industry_group == str(kind)].set_index(\"year\")\n",
    "    \n",
    "    out_df[\"inflation_adj\"] = out_df.apply(lambda row: \n",
    "                                                convert_to_year(row.avg_annual_pay_2015, \n",
    "                                                row.name, \n",
    "                                                2015),\n",
    "                                                axis=1)\n",
    "    \n",
    "    return out_df\n",
    "\n",
    "def state_comparison(state1, state2, Show_actual = True):\n",
    "    kind = 'crops'\n",
    "    # Make a temporary frame for the state and reset index so\n",
    "    # years are their own column\n",
    "    state1_df = make_df(state1,kind)\n",
    "    state1_df = state1_df.reset_index()\n",
    "    if state2:\n",
    "        state2_df = make_df(state2,kind)\n",
    "        state2_df = state2_df.reset_index()\n",
    "    fig = plt.figure(figsize=(12, 8))\n",
    "   \n",
    "    plt.plot(state1_df['year'], state1_df['inflation_adj'])\n",
    "    \n",
    "    difference1 = state1_df.at[25, \"inflation_adj\"]-state1_df.at[0, \"inflation_adj\"]\n",
    "        \n",
    "    plt.legend([ \"Adjusted to 2015 dollars\", \"No Adjustment for Inflation\"])\n",
    "    plt.ylabel(\"US Dollars\", size =16)\n",
    "    plt.xlabel(\"Year\", size=16)\n",
    "    # Should we want to look at another state, we plot that too and\n",
    "    # adjust the trace legends\n",
    "    \n",
    "    if state2:\n",
    "        plt.plot(state2_df['year'], state2_df['inflation_adj'])\n",
    "\n",
    "    else:\n",
    "         plt.legend([\"Adjusted to 2015 dollars\",\"No Adjustment for Inflation\"])\n",
    "            \n",
    "    if Show_actual:\n",
    "        plt.plot(state1_df['year'], state1_df['avg_annual_pay_2015'])\n",
    "        plt.legend([\"Adjusted to 2015 dollars\",\"No Adjustment for Inflation\"])\n",
    "        if state2:\n",
    "            plt.plot(state2_df['year'], state2_df['avg_annual_pay_2015'])\n",
    "            \n",
    "    \n",
    "    if state2:\n",
    "        difference2 = state2_df.at[25, \"inflation_adj\"]-state2_df.at[0, \"inflation_adj\"]\n",
    "        plt.legend([\n",
    "            ' '.join([state1.rstrip(\" Statewide\"), \"Adjusted to 2015 dollars\"]),\n",
    "            ' '.join([state2.rstrip(\" Statewide\"), \"Adjusted to 2015 dollars\"]),\n",
    "            ' '.join([state1.rstrip(\" Statewide\"),\"No Adjustment for Inflation\"]),\n",
    "            ' '.join([state2.rstrip(\" Statewide\"),\"No Adjustment for Inflation\"])\n",
    "          \n",
    "        ])\n",
    "        \n",
    "        plt.title(\" \".join([state1.rstrip(\" Statewide\"),\n",
    "                            \"Change in pay (2015 dollars) = $\",\n",
    "                            str(round(difference1,2)), '\\n',\n",
    "                            state2.rstrip(\" Statewide\"),\n",
    "                            \"Change in pay (2015 dollars) = $\",\n",
    "                            str(round(difference2,2))]))\n",
    "    else:\n",
    "         plt.title(\" \".join([state1.rstrip(\" Statewide\"),\n",
    "                            \"Change in pay (2015 dollars) = $\",\n",
    "                            str(round(difference1,2))]))\n",
    "        \n",
    "                            \n",
    "    plt.show()\n",
    "    \n",
    "    \n",
    "choices = list(state_df[\"area_title\"].unique())[0:-1]\n",
    "choices.remove('District of Columbia')\n",
    "choices2 = [None] + choices\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "HTML('''<script>\n",
    "    code_show=true; \n",
    "    function code_toggle() {\n",
    "        if (code_show){\n",
    "            $('div.cell.code_cell.rendered.selected div.input').hide();\n",
    "        } else {\n",
    "            $('div.cell.code_cell.rendered.selected div.input').show();\n",
    "        }\n",
    "        code_show = !code_show\n",
    "    } \n",
    "\n",
    "    $( document ).ready(code_toggle);\n",
    "    </script>\n",
    "\n",
    "    To see the algorithm which generates this graph, click <a href=\"javascript:code_toggle()\">here</a>.''')\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "interactive(state_comparison, state1=choices, state2=choices2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using the widget above, you can compare the farmers pay between to states and view the un-altered pay scales by clicking `Show_actual`. You may also adjust for inflation.  If you compare California to Minnesota for example, you'll notice that while wages increased in both states, Minnesota pays farmers consistently more than California. \n",
    "\n",
    "## Things to consider from the plot above\n",
    "Check the traces for any states you're interested in. One question that you may ask could come from the trace of Hawaii once adjusted for inflation. Farmer's ages there declined from 1995 to 2004. A great research question for students may be to investigate _why_ that may be the case and what events lead to that change. Additionally, Hawaii bounces back after 2005, what events took place that could cause that? \n",
    "\n",
    "Other interesting questions could be more basic, such as the actual wages vs. inflation, or even comparing two states. Certainly, an interesting case study may be Puerto Rico, however that will be shown more directly in the next section.\n",
    "\n",
    "## Which states have the highest and lowest farmer wages?\n",
    "\n",
    "With all this data that seems to be a natural next question to ask, and perhaps an interesting one to ask students to figure out. Using the our already established data frame, finding these answers are straight forward. They are demonstrated below. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "crops_df = state_df[state_df.industry_group == 'crops'].dropna(subset=[\"avg_annual_pay_2015\"])\n",
    "\n",
    "# This adds a column of inflation adjusted values.\n",
    "crops_df[\"inflation_adj\"] = crops_df.apply(lambda row: \n",
    "                                           convert_to_year(row.avg_annual_pay_2015, \n",
    "                                           row.year, \n",
    "                                            2015),\n",
    "                                            axis=1)\n",
    "\n",
    "\n",
    "YEAR = 1990\n",
    "\n",
    "\n",
    "\n",
    "# As we've set the index of our table to 'year', we can now pull values from a specified \n",
    "# column at a given year using the 'at' function like so \n",
    "\n",
    "min_pay = crops_df[crops_df.year == YEAR][\"inflation_adj\"].min()\n",
    "max_pay = crops_df[crops_df.year == YEAR][\"inflation_adj\"].max()\n",
    "\n",
    "\n",
    "print(\"Minimum pay in\", YEAR, \"was $\", min_pay)\n",
    "print(\"Maximum pay in\", YEAR, \"was $\", max_pay)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Here we have found the minimum and maximum pay in 1990 in 2015 equivalent dollars.\n",
    "\n",
    "---\n",
    "### Try it out\n",
    "- Which variable needs to be adjusted to see different years? Try finding the minimum and maximum pay in 2015 by modifying the cell above. \n",
    "- What do you think you have to modify to see minimum and maximum pay without inflation adjustments? View the non-adjusted wages in 1990 by modifying the cell above.\n",
    "---\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## Putting it all together in a table\n",
    "Now, minimum and maximum values are great, but it will be more interesting if we're able to pull certain values, and place them in a table for further analysis. One such analysis that might be of interest is for each year, which state payed its farmers the most and the least? This is another task that can be accomplished using Python and Jupyter notebooks. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# First, we add a new column which adjusts each wage to 2015 as we did before. \n",
    "\n",
    "# First, we sort our data frame in ascending order by their average anual pay\n",
    "# before inflation adjustments \n",
    "\n",
    "# Add column which adjusts for inflation\n",
    "\n",
    "crops_df = crops_df.sort_values(\"avg_annual_pay_2015\")\n",
    "crops_df[\"inflation_adj\"] = crops_df.apply(lambda row: \n",
    "                                            convert_to_year(row.avg_annual_pay_2015, \n",
    "                                            row.year, \n",
    "                                            2015),\n",
    "                                            axis=1)\n",
    "\n",
    "\n",
    "# Choose the columns we care about by name\n",
    "columns_to_see = [\"year\",\"area_title\",\"avg_annual_pay_2015\",\"inflation_adj\"]\n",
    "\n",
    "\n",
    "# As our data is already sorted, we now group our data by year, and choose whatever value \n",
    "# in each group using .nth(number)\n",
    "# Note: Group 0 will be the smallest values, group 1 will be the second smallest etc\n",
    "# to find the largest, simply enter -1, to find the second largest enter -2, etc\n",
    "\n",
    "# Note, nth only finds the \"nth position in the list\". Here it cooresponds to minimum and maximum values \n",
    "# because we shorted our data table by value.\n",
    "\n",
    "\n",
    "crops_df[columns_to_see].groupby('year').nth(-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Where in the table above we can see the highest and lowest (and everything in between) workers in each year. \n",
    "\n",
    "---\n",
    "### Try it out\n",
    "- The cell above currently prints out the highest paid states in each year. To view the lowest paid states try the following\n",
    "    1. In the cell above find the line which prints the highest paid states \n",
    "    ```python\n",
    "     crops_df[columns_to_see].groupby('year').nth(-1)\n",
    "    ```\n",
    "    and change it to \n",
    "    ```python\n",
    "    crops_df[columns_to_see].groupby('year').nth(0)\n",
    "    ```\n",
    "    to print the lowest paid states. In this case, the `-1` specifies the last element of the list, `-2` would specify the second last element etc. \n",
    "    \n",
    "    2. Change the same line to \n",
    "    ```python\n",
    "       crops_df[columns_to_see].groupby('year').nth(1)\n",
    "    ```\n",
    "    to view the second lowest paid state\n",
    "    \n",
    "    3. Which state made the second highest wage in 2014?\n",
    "    \n",
    "---\n",
    "\n",
    "## Creating a different kind of interactive plot\n",
    "This might be more interesting to see summarized in a plot which also includes mouse hover interactivity so we can get more information from it. This is summarized below.\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "hide_me\n",
    "plot_df = crops_df[columns_to_see].groupby('year').nth(-1)\n",
    "\n",
    "# Here is an example of how a plot which hover labels is made \n",
    "traces = []\n",
    "# Here we put out the names of the traces we want to put on the graph\n",
    "names = [\"Lowest Paid\", \"Second Lowest Paid\", \"Second Highest Paid\", \"Highest Paid\"]\n",
    "\n",
    "# These numbers represent the lowest, second lowest, second highest and highest paid\n",
    "# farmers from the plot_df with the grouping that we saw eariler. \n",
    "\n",
    "for j, i in enumerate([0,1,-2,-1]):\n",
    "    plot_df = crops_df[columns_to_see].groupby('year').nth(i) \n",
    "    \n",
    "    # Here we're adding a trace onto the end of our data frame\n",
    "    # where index is the year \n",
    "    traces.append(go.Scatter(\n",
    "      x=plot_df.index,\n",
    "      y=plot_df.inflation_adj,\n",
    "      text = plot_df.area_title,\n",
    "      name = names[j]\n",
    "    ))\n",
    "\n",
    "# Here we're formatting axis labels\n",
    "layout = go.Layout(\n",
    "    title='Sorted Farm Wages (2015 Dollars)',\n",
    "    xaxis=dict(\n",
    "        title='Year',\n",
    "        titlefont=dict(\n",
    "            size=18,\n",
    "            color='#7f7f7f'\n",
    "        )\n",
    "    ),\n",
    "    yaxis=dict(\n",
    "        title='Inflation Adjusted Wages (USD)',\n",
    "        titlefont=dict(\n",
    "            size=18,\n",
    "            color='#7f7f7f'\n",
    "        )\n",
    "    )\n",
    ")\n",
    "\n",
    "fig = go.Figure(data=go.Data(traces), layout=layout)\n",
    "    \n",
    "iplot(fig)\n",
    "\n",
    "HTML('''<script>\n",
    "    code_show=true; \n",
    "    function code_toggle() {\n",
    "        if (code_show){\n",
    "            $('div.cell.code_cell.rendered.selected div.input').hide();\n",
    "        } else {\n",
    "            $('div.cell.code_cell.rendered.selected div.input').show();\n",
    "        }\n",
    "        code_show = !code_show\n",
    "    } \n",
    "\n",
    "    $( document ).ready(code_toggle);\n",
    "    </script>\n",
    "\n",
    "    To see the algorithm which generates this graph, click <a href=\"javascript:code_toggle()\">here</a>.''')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Using a plot like this it is easy to see how the maximum and minimum farmer's wages have changed accross the united states from 1990 to 2015. It is also very simple to hover your mouse over top of the graph and extra information can be digested from the plot. \n",
    "\n",
    "Interesting questions that could be asked to students from this plot could surround what circumstances contribute to Puerto Rico farmers being paid less than the other farmers in the United States. Often considerably less than  the next lowest payed state to the order of $5000 dollars a year. Five thousand dollars a year is about 1/3 of Puerto Rico's maximum farmer's wage. \n",
    "\n",
    "\n",
    "Plots like this can make it interactive and natural to get students to investigate the data. Perhaps there's a trend that stands out to them - a social issue that is the cause for discrepancies between states or large fluctuations in wage from year to year. Indeed, if you find this notebook interesting, we can create something similar for use in your classroom. Provided the data exists of course.\n",
    "\n",
    " You can have your students interact directly with the data in a way you've seen here, or we can hide all the coding behind the scenes like we have done with the widget and the plot above. How much direct interaction you want your students to have with Python to make it through the lesson/assignment is entirely up to you.  \n",
    "\n",
    "# Take away \n",
    "\n",
    "In this notebook we demonstrated how an existing social science application can be extended in your classroom in a way that will help demonstrate computational thinking, critical thinking, and research skills. It also demonstrated how you can use existing data sets to further analysis on different problems."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "![alt text](https://github.com/callysto/callysto-sample-notebooks/blob/master/notebooks/images/Callysto_Notebook-Banners_Bottom_06.06.18.jpg?raw=true)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
